package leetcode.剑指offer.后40;

import org.junit.Test;

import java.text.DecimalFormat;
import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/8/1 22:56
 * @description：https://leetcode-cn.com/problems/nge-tou-zi-de-dian-shu-lcof/
 */
public class 剑指_60_n个骰子的点数 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(Arrays.toString(solution.dicesProbability(11)));
    }

    /*
    class Solution {
        Map<Integer, Double> map = new HashMap<>();
        public double[] dicesProbability(int n) {
            dfs(n, 1.0, 0);
            double[] ans = new double[map.size()];
            int i = 0;
            for(Map.Entry<Integer, Double> entry : map.entrySet()) {
                DecimalFormat df = new DecimalFormat("0.00000");
                ans[i++] = Double.parseDouble(df.format(entry.getValue()));
                System.out.println(entry.getKey() + " " + ans[i - 1]);
            }
            return ans;
        }

        private void dfs(int n, double factor, int cur) {
            if (n == 0) {
                map.put(cur, map.getOrDefault(cur, 0.0) + factor);
                return;
            }
            n--;
            for (int i = 1; i <= 6; i++) {
                dfs(n, 1 / 6.0 * factor, cur + i);
            }
        }

    }
     */

    class Solution {
        public double[] dicesProbability(int n) {
            double[] dp = new double[6];
            Arrays.fill(dp, 1.0 / 6.0);
            for (int i = 2; i <= n; i++) {
                double[] tmp = new double[i * 5 + 1];
                for (int j = 0; j < dp.length; j++) {
                    for (int k = 0; k < 6; k++) {
                        tmp[j + k] += dp[j] / 6.0;
                    }
                }
                dp = tmp;
            }
            return dp;
        }
    }


}